<?php
// +----------------------------------------------------------------------
// | Jqcms [ Avenue to Jane ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://jqcms.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: hyacinth <845077398@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\BaseController;

class ArticleController extends BaseController {
      
    //列表
    public function index(){
       
        $article = D('Article');
        if(IS_POST){
           $attr = '%'.I('s_isvouch').'%'.I('s_istop').'%';
           $where['attr'] = array('like',$attr);
           $where['classid'] = I('cid');
           $where['title'] = array('like','%'.I('keywords').'%');
        }else{
           $where = ""; 
        }
        $count = $article->where($where)->count();
        $p = getpage($count,10);
        $list = $article->field(true)->where($where)->order('sort desc,id desc')->limit($p->firstRow, $p->listRows)->select();
        $this->assign('cate',$article->getCategory());
        $this->assign('list', $list); 
        $this->assign('page', $p->show());
        $this->display();
        
    }
    //添加
    public function create(){
        
        $article = D('Article');
        if(IS_POST){
            
            if($article->create()){
                $aid = $article->add();
                $article->setContent($aid);
                $this->success('添加成功!','index');
            }else{
                $this->error($article->getError());  
            }
            
        }else{
            $data['cate'] = $article->getCategory();
            $data['piccf'] = $article->getPicsize();
            $this->assign('data',$data);
            $this->display();
        }
           
    }
    //编辑
    public function edit($id){
        
        $article = D('Article');
        if (IS_POST) {
            
            if($article->create()){
                $article->save();
                $article->setContent();
                $this->success('编辑成功!','index');
            }else{
                $this->error($article->getError());
            }
            
        }else {
            //读取数据
            $data = $article->find($id);
            $data['content'] = $article->getContent($data['id']);
            $data['cate'] = $article->getCategory();
            $data['piccf'] = $article->getPicsize();
            $data['tagdata'] = $article->getTag($data['tag']);
            $data['attr'] = $article->getAttr($data['attr']);
            $this->assign('data',$data);
            $this->display();
        }
        
    }
   
    //单删 
    public function del($id){
    
        $article = D('Article');
        $content = M('Content');
        $deccount = $article->setDectag($id);
        $del = $article->delete($id);
        $delc = $content->where(array('aid'=>$id))->delete();
  
        if($del !==0 && $del !==false){
            $this->success('删除成功!');
        }else {
            $this->error('删除失败!');
        }
         
    }
    
    //批量删除
    public function delselect(){
        
        $article = D('Article');
        $content = M('Content');
        $idarr = I('id');
        $idstr = implode(',',$idarr);
        foreach ($idarr as $v){
            $article->setDectag($v);
        }
        $del = $article->delete($idstr);
        $decid['aid'] = array('in',$idstr); 
        $delc = $content->where($decid)->delete();
        
        if($del !==0 && $del !==false){
            $this->success('删除成功!');
        }else {
            $this->error('删除失败!');
        }
         
    }
    
    //批量排序
    public function sortselect(){
    
        $article = M('Article');
        $idarr = I('id');
        $sortarr = I('sort');
        for ($x=0; $x<=count($idarr); $x++){
            $where['id'] = $idarr[$x];
            $data['sort'] = $sortarr[$x];
            $sort = $article->where($where)->field('sort')->save($data);
        }
        $this->success('更新成功!');
        
    }
    //批量推荐
    public function vouchattr(){
        
        $article = M('Article');
        $idarr = I('id');
        foreach ($idarr as $v){
            $attr = $article->field('attr')->find($v);
            $attr = explode(',', $attr['attr']);
            $attr[0] = I('isvouch');
            $attr = implode(',', $attr);
            $article->where(array('id'=>$v))->setField('attr',$attr);    
        }
        $this->success('设置成功!');
        
    }
    //批量置顶
    public function topattr(){
    
        $article = M('Article');
        $idarr = I('id');
        foreach ($idarr as $v){
            $attr = $article->field('attr')->find($v);
            $attr = explode(',', $attr['attr']);
            $attr[1] = I('istop');
            $attr = implode(',', $attr);
            $article->where(array('id'=>$v))->setField('attr',$attr);
        }
        $this->success('设置成功!');
    
    }
   //批量移动
   public function movecate(){
       
       $article = M('Article');
       $idarr = I('id');
       foreach ($idarr as $v){
           $article->where(array('id'=>$v))->setField('classid',I('movecid'));
       }
       $this->success('移动成功!');
       
   }
   //批量复制
   public function copyid(){
       
       $article = D('Article');
       $content = M('Content');
       $idarr = I('id');
       $id = $idarr[0]; 
       $data = $article->find($id);
       $contdata = $content->where(array('aid'=>$data['id']))->find(); 
       unset($data['id'],$contdata['id']);
       
       for ($x=0; $x<I('copynum'); $x++){
           $artid = $article->add($data);
           $contdata['aid'] = $artid;
           $content->add($contdata);
           $article->setInctag($id);
       }
       $this->success('复制成功!');
       
   }
   
    
  
    
}